﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeBuilder.Models.TemplateModels;
using CodeBuilder.Templates;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using RazorEngine.Configuration;
using RazorEngine.Templating;
using Engine = RazorEngine.Engine;
using Task = System.Threading.Tasks.Task;

namespace CodeBuilder
{
    public partial class CommandBase
    {
        #region 代码生成
        
        #region 生成前端
        /// <summary>
        /// 创建前端view文件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="name"></param>
        /// <param name="frontPath"></param>
        protected void CreateFrontFile(DtoFileModel model, string name, string frontPath)
        {
            string index_vue = Engine.Razor.RunCompile("Front.Vue.View.IndexTemplate", typeof(DtoFileModel), model);
            string fileName_index_vue = $"index.vue";
            AddFileToDirectory(frontPath, index_vue, fileName_index_vue);

            string createOrUpdate_vue = Engine.Razor.RunCompile("Front.Vue.View.CreateOrUpdateTemplate", typeof(DtoFileModel), model);
            string fileName_createOrUpdate_vue = $"CreateOrUpdate{name}.vue";
            var componentsPath = Path.Combine(frontPath, "components");
            if (!Directory.Exists(componentsPath)) Directory.CreateDirectory(componentsPath);
            AddFileToDirectory(componentsPath, createOrUpdate_vue, fileName_createOrUpdate_vue);
        }

        /// <summary>
        /// 创建前端Store文件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="name"></param>
        /// <param name="frontPath"></param>
        protected void CreateStoreFile(DtoFileModel model, string name, string frontStorePath)
        {
            string index_vue = Engine.Razor.RunCompile("Front.Vue.Store.ModulesTemplate", typeof(DtoFileModel), model);
            string fileName_store_js = $"{name.FirstCharToLower()}.js";
            AddFileToDirectory(frontStorePath, index_vue, fileName_store_js);
        }


        /// <summary>
        /// 编辑路由
        /// </summary>
        /// <param name="frontPath"></param>
        /// <param name="name"></param>
        protected void EditRouter(string frontPath, string name)
        {
            string moduleCode = "{\r\n";
            moduleCode += $"      path: '{name.FirstCharToLower()}',\r\n";
            moduleCode += $"      name: '{name}',\r\n";
            moduleCode += $"      component: () => import('@/views/other/{name.ToHyphenate()}/index'),\r\n";
            moduleCode += "      meta: {\r\n";
            moduleCode += $"        title: '{name}',\r\n";
            moduleCode += $"        permission: 'Pages.{name}'\r\n";
            moduleCode += "      }\r\n";
            moduleCode += "    },\r\n";
            moduleCode += "    // {#insert module code#}";

            string routerFilePath = frontPath + "router\\modules\\otherModule.js";
            string routerContent = File.ReadAllText(routerFilePath);
            routerContent = routerContent.Replace("// {#insert module code#}", moduleCode);

            AddFileToDirectory(routerFilePath, routerContent);

        }

        /// <summary>
        /// 修改Store
        /// </summary>
        /// <param name="frontPath"></param>
        /// <param name="name"></param>
        protected void EditStoreIndex(string frontPath, string name)
        {
            string importCode = $"import {name.FirstCharToLower()} from './modules/{name.FirstCharToLower()}'\r\n";
            importCode += "// {#insert import code#}";

            string moduleCode = $",\r\n    {name.FirstCharToLower()}";
            moduleCode += "// {#insert module code#}";

            string storeFilePath = frontPath + "store\\index.js";
            string storeContent = File.ReadAllText(storeFilePath);
            storeContent = storeContent.Replace("// {#insert import code#}", importCode);
            storeContent = storeContent.Replace("// {#insert module code#}", moduleCode);

            AddFileToDirectory(storeFilePath, storeContent);
        }

        /// <summary>
        /// 导入并注入模块
        /// </summary>
        /// <param name="frontPath"></param>
        /// <param name="name"></param>
        protected void EditModule(string frontPath, string name, string dirPath)
        {
            string importCode = "import { " + name + "Component } from './" + dirPath.Replace("\\", ".").ToLower() + "/" + name.ToLower() + ".component';\r\n";
            importCode += "import { CreateOrEdit" + name + "ModalComponent } from './" + dirPath.Replace("\\", ".").ToLower() + "/create-or-edit-" + name.ToLower() + "-modal.component';\r\n";
            importCode += "// {#insert import code#}\r\n";

            string declarationsCode = name + "Component,\r\n";
            declarationsCode += "    CreateOrEdit" + name + "ModalComponent,\r\n";
            declarationsCode += "    // {#insert declarations code#}\r\n";

            string entryComponentsCode = name + "Component,\r\n";
            entryComponentsCode += "    CreateOrEdit" + name + "ModalComponent,\r\n";
            entryComponentsCode += "    // {#insert declarations code#}\r\n";

            string moduleFilePath = frontPath + "app\\admin\\admin.module.ts";
            string moduleContent = File.ReadAllText(moduleFilePath);
            moduleContent = moduleContent.Replace("// {#insert import code#}", importCode);
            moduleContent = moduleContent.Replace("// {#insert declarations code#}", declarationsCode);
            moduleContent = moduleContent.Replace("// {#insert entryComponents code#}", entryComponentsCode);

            AddFileToDirectory(moduleFilePath, moduleContent);
        }

        /// <summary>
        /// 添加路由，目前都是加到admin子路由中
        /// </summary>
        /// <param name="frontPath"></param>
        /// <param name="name"></param>
        protected void EditRouter(string frontPath, string name, string dirPath)
        {
            string importCode = "import { " + name + "Component } from './" + dirPath.Replace("\\", ".").ToLower() + "/" + name.ToLower() + ".component';\r\n";
            importCode += "// {#insert import code#}\r\n";

            string routesCode = "{ path: '" + name.ToLower() + "', component: " + name + "Component, data: { permission: 'Pages." + name + "' } },\r\n";
            routesCode += "    // {#insert routes code#}\r\n";

            string routerFilePath = frontPath + "app\\admin\\admin-routing.module.ts";
            string routerContent = File.ReadAllText(routerFilePath);
            routerContent = routerContent.Replace("// {#insert import code#}", importCode);
            routerContent = routerContent.Replace("// {#insert routes code#}", routesCode);

            AddFileToDirectory(routerFilePath, routerContent);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="frontPath"></param>
        /// <param name="cnName"></param>
        /// <param name="name"></param>
        protected void AddMenu(string frontPath, string cnName, string name)
        {
            string importCode = "new AppMenuItem('" + cnName + "', 'Pages." + name + "', 'anticon anticon-setting', '/app/admin/" + name.ToLower() + "'),\r\n";
            importCode += "                // {#insert menu code#}\r\n";

            string menuFilePath = frontPath + "app\\shared\\layout\\nav\\app-navigation.service.ts";
            string menuContent = File.ReadAllText(menuFilePath);
            menuContent = menuContent.Replace("// {#insert menu code#}", importCode);

            AddFileToDirectory(menuFilePath, menuContent);
        }

        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="frontPath"></param>
        /// <param name="name"></param>
        protected void AddProxy(string frontPath, string name)
        {
            string routesCode = "ApiServiceProxies." + name + "ServiceProxy,\r\n";
            routesCode += "        // {#insert routes code#}\r\n";

            string proxyFilePath = frontPath + "shared\\service-proxies\\service-proxy.module.ts";
            string proxyContent = File.ReadAllText(proxyFilePath);
            proxyContent = proxyContent.Replace("// {#insert proxy code#}", routesCode);

            AddFileToDirectory(proxyFilePath, proxyContent);
        }
        #endregion
        #endregion


    }
}
